package com.irdstudio.efp.console.api.rest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.irdstudio.basic.framework.core.constant.PubConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.irdstudio.efp.console.common.ConsoleConstant;
import com.irdstudio.efp.console.service.facade.PrdApplyRuleService;
import com.irdstudio.efp.console.service.facade.PrdInfoApplyService;
import com.irdstudio.efp.console.service.vo.PrdApplyRuleVO;
import com.irdstudio.efp.e4a.service.facade.SRoleDataRuleService;
import com.irdstudio.basic.framework.core.constant.ApplicationConstance;
import com.irdstudio.basic.framework.core.constant.DataRuleType;
import com.irdstudio.basic.framework.core.constant.ResponseData;
import com.irdstudio.basic.framework.web.controller.AbstractController;

@RestController
@RequestMapping("/api")
public class PrdApplyRuleController extends AbstractController {

	@Autowired
	@Qualifier("prdApplyRuleService")
	private PrdApplyRuleService prdApplyRuleService;
	@Autowired
	@Qualifier("prdInfoApplyService")
	private PrdInfoApplyService prdInfoApplyService;
	@Autowired
	@Qualifier("sRoleDataRuleService")
	private SRoleDataRuleService sRoleDataRuleService;

	/**
	 * 列表数据查询
	 * 
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value = "/prd/apply/rules", method = RequestMethod.POST)
	public @ResponseBody ResponseData<List<PrdApplyRuleVO>> queryPrdApplyRuleAll(@RequestBody PrdApplyRuleVO vo) {
		// 根据用户角色获取用户权限方案
		List<PrdApplyRuleVO> outputVo = null;
		Map<String,String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		vo.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PRD_CODE));
		DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
		setUserInfoToVO(vo);

		// 根据权限方案调用不同的方案进行数据查询
		switch (dataRuleType) {
		case Owner:
			outputVo = prdApplyRuleService.queryAllOwner(vo);
			break;
		case CurrOrg:
			outputVo = prdApplyRuleService.queryAllCurrOrg(vo);
			break;
		case CurrDownOrg:
			outputVo = prdApplyRuleService.queryAllCurrDownOrg(vo);
			break;
		default:
			outputVo = prdApplyRuleService.queryAllOwner(vo);
			break;
		}
		return getResponseData(outputVo);
	}

	/**
	 * 更具bizSerno流水号查询产品申请规则关联信息
	 * 
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value = "/prd/apply/rulesBySerno", method = RequestMethod.POST)
	public @ResponseBody ResponseData<List<PrdApplyRuleVO>> queryAllByBizSerno(@RequestBody PrdApplyRuleVO vo) {
		// 查询是否有数据权限
		Map<String, String> key = new HashMap<>();
		key.put("bizSerno", vo.getBizSerno());

		boolean checkRLT = this.checkOptionalAuthority(key, ConsoleConstant.URL_DATA_PERMISS_PRD_APPLY, this.prdInfoApplyService);
		if (!checkRLT)
			return ResponseData.create(null, ApplicationConstance.NO_AUTHORITY, ApplicationConstance.NO_AUTHORITY_MESSAGE, false);
		List<PrdApplyRuleVO> outputVo = prdApplyRuleService.queryAllByBizSerno(vo);
		return getResponseData(outputVo);
	}

	/**
	 * 根据主键查询详情
	 * 
	 * @return
	 */
	@RequestMapping(value = "/prd/apply/rule/{bizSerno}", method = RequestMethod.GET)
	public @ResponseBody ResponseData<PrdApplyRuleVO> queryByPk(@PathVariable("bizSerno") String bizSerno) {
		PrdApplyRuleVO inVo = new PrdApplyRuleVO();
		inVo.setBizSerno(bizSerno);
		PrdApplyRuleVO outputVo = prdApplyRuleService.queryByPk(inVo);
		return getResponseData(outputVo);

	}

	/**
	 * 根据主键删除信息
	 * 
	 * @param prdApplyRule
	 * @return
	 */
	@RequestMapping(value = "/prd/apply/rule", method = RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody PrdApplyRuleVO inPrdApplyRuleVo) {
		int outputVo = prdApplyRuleService.deleteByPk(inPrdApplyRuleVo);
		return getResponseData(outputVo);

	}

	/**
	 * 根据主键更新信息
	 * 
	 * @param inPrdApplyRuleVo
	 * @return
	 */
	@RequestMapping(value = "/prd/apply/rule", method = RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody PrdApplyRuleVO inPrdApplyRuleVo) {
		int outputVo = prdApplyRuleService.updateByPk(inPrdApplyRuleVo);
		return getResponseData(outputVo);

	}

	/**
	 * 新增数据
	 * 
	 * @param inPrdApplyRuleVo
	 * @return
	 */
	@RequestMapping(value = "/prd/apply/rule", method = RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertPrdApplyRule(@RequestBody PrdApplyRuleVO inPrdApplyRuleVo) {
		int outputVo = prdApplyRuleService.insertPrdApplyRule(inPrdApplyRuleVo);
		return getResponseData(outputVo);

	}
}
